package org.thebedells.xcodelate.migration;

import com.webobjects.eocontrol.EOEditingContext;
import com.webobjects.foundation.NSArray;

import er.extensions.migration.ERXMigrationDatabase;
import er.extensions.migration.ERXMigrationTable;
import er.extensions.migration.ERXModelVersion;

public class EOMXcodeLate0 extends ERXMigrationDatabase.Migration {

  @Override
  public NSArray<ERXModelVersion> modelDependencies() {
    return null;
  }

  @Override
  public void downgrade(EOEditingContext editingContext, ERXMigrationDatabase database) throws Throwable {
    // DO NOTHING
  }

  @Override
  public void upgrade(EOEditingContext editingContext, ERXMigrationDatabase database) throws Throwable {
    ERXMigrationTable stringTableTable = database.newTableNamed("StringTable");
    stringTableTable.newStringColumn("filename", 255, false);
    stringTableTable.newIntegerColumn("id", false);
    stringTableTable.newIntegerColumn("projectId", false);
    stringTableTable.create();
    stringTableTable.setPrimaryKey("id");
    stringTableTable.addForeignKey("projectId", "Project", "id");

    ERXMigrationTable userTable = database.newTableNamed("User");
    userTable.newIntegerColumn("id", false);
    userTable.newStringColumn("passwordHash", 50, false);
    userTable.newStringColumn("username", 50, false);
    userTable.create();
    userTable.setPrimaryKey("id");

    ERXMigrationTable projectTable = database.newTableNamed("Project");
    projectTable.newIntegerColumn("id", false);
    projectTable.newIntegerColumn("nativeLanguageId", false);
    projectTable.newIntegerColumn("ownerId", false);
    projectTable.newStringColumn("projectName", 100, false);
    projectTable.create();
    projectTable.setPrimaryKey("id");
    projectTable.addForeignKey("nativeLanguageId", "Language", "id");
    projectTable.addForeignKey("ownerId", "User", "id");

    ERXMigrationTable languageTable = database.newTableNamed("Language");
    languageTable.newStringColumn("appleLangCode", 16, false);
    languageTable.newIntegerColumn("id", false);
    languageTable.newStringColumn("isoCode", 10, false);
    languageTable.newStringColumn("languageName", 100, false);
    languageTable.create();
    languageTable.setPrimaryKey("id");

    ERXMigrationTable stringsTable = database.newTableNamed("Strings");
    stringsTable.newIntegerColumn("id", false);
    stringsTable.newIntegerColumn("languageId", false);
    stringsTable.newIntegerColumn("rating", false);
    stringsTable.newIntegerColumn("sourceStringId", true);
    stringsTable.newBlobColumn("stringValue", false);
    stringsTable.newIntegerColumn("submitedUserId", false);
    stringsTable.create();
    stringsTable.setPrimaryKey("id");
    stringsTable.addForeignKey("languageId", "Language", "id");
    stringsTable.addForeignKey("sourceStringId", "Strings", "id");
    stringsTable.addForeignKey("submitedUserId", "User", "id");

    ERXMigrationTable strings__StringsTableTable = database.newTableNamed("Strings__StringsTable");
    strings__StringsTableTable.newIntegerColumn("stringsId", false);
    strings__StringsTableTable.newIntegerColumn("stringTableId", false);
    strings__StringsTableTable.create();
    strings__StringsTableTable.setPrimaryKey("stringTableId", "stringsId");
    strings__StringsTableTable.addForeignKey("stringsId", "Strings", "id");
    strings__StringsTableTable.addForeignKey("stringTableId", "StringTable", "id");

  }
}
